package com.sanglh.tutorial.main;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.sanglh.tutorial.R;
import com.sanglh.tutorial.androidtouch.AndroidTouchService;
import com.sanglh.tutorial.asynctask.AsyncActivity;
import com.sanglh.tutorial.common.base.BaseListAdapter;
import com.sanglh.tutorial.extra.ExtraActivity;
import com.sanglh.tutorial.image.ImageActivity;
import com.sanglh.tutorial.settings.SettingsActivity;
import com.sanglh.tutorial.thread.ThreadActivity;
import com.sanglh.tutorial.windowmanager.WindowManagerActivity;

public class MainActivity extends ActionBarActivity implements
		OnItemClickListener {
	ArrayList<MainEntry> entries = new ArrayList<>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		loadData();
	}

	private void loadData() {
		entries.add(new MainEntry("Window Manager", WindowManagerActivity.class));
		entries.add(new MainEntry("Settings", SettingsActivity.class));
		entries.add(new MainEntry("Thread", ThreadActivity.class));
		entries.add(new MainEntry("AsyncTask", AsyncActivity.class));
		entries.add(new MainEntry("Image", ImageActivity.class));
		entries.add(new MainEntry("Extra", ExtraActivity.class));
		entries.add(new MainEntry("IOSTouch", AndroidTouchService.class));
		ListView lv = (ListView) findViewById(R.id.lv_main);
		BaseListAdapter<MainEntry> adapter = new BaseListAdapter<MainEntry>(
				this, entries, R.layout.row_base);
		lv.setAdapter(adapter);
		lv.setOnItemClickListener(this);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Intent intent = new Intent(MainActivity.this,
				entries.get(position).clazz);
		try {
			startActivity(intent);
		} catch (Exception e) {
			startService(intent);
		}
	}

}
